//----------car对象
function Car(carColor,carDire,carNum){
	var obj=new Object();
	obj.carColor=carColor
	obj.carDire=carDire
	obj.carNum=carNum
	obj.speed=SpeedType
	obj.imgWidth=0
	obj.imgHeigh=0
	obj.loop=1
	obj.carPointx=1
	obj.carPointy=1
	obj.id=carNumber
	obj.isstop=0
	obj.stoptime=0
	carNumber++
	
	obj.carPointx=returnPoint(obj.carDire,obj.carNum).x
	
	obj.carPointy=returnPoint(obj.carDire,obj.carNum).y
	
	
	
        
	
	
	obj.paint=function(ctx){
	     var imgSrc="img/"+obj.carDire+"/"+obj.carColor+".png"
	     
	     preImage(imgSrc,function(){
	     	ctx.drawImage(this,obj.carPointx,obj.carPointy,obj.imgWidth,obj.imgHeigh)
	     })
	     imgSrc=null
	     //------画图
	}
	obj.move=function(ctx){
		//分组判断 进行求速度
	var _this=gameMonitor
	switch(obj.carDire){
		case carDirection[0]:
		     obj.carPointy+=obj.loop*obj.speed;
		     break;
		case carDirection[1]:
		     obj.carPointx-=obj.loop*obj.speed;
		     break;
		case carDirection[2]:
		     obj.carPointy-=obj.loop*obj.speed;
		     break;
		case carDirection[3]:
		     obj.carPointx+=obj.loop*obj.speed;  
		     break;
	}
	
	//判断有无车辆跑出规定范围
	if(carNorthOneList.length>0&&carNorthOneList[0].carPointy>_this.canvasHeigh){
		var temp=[];
		carNorthOneList[0]==null
		for(i=1;i<carNorthOneList.length;i++){
			temp[i-1]=carNorthOneList[i]
		}
		carNorthOneList=temp
		temp=null
	}
	if(carNorthTwoList.length>0&&carNorthTwoList[0].carPointy>_this.canvasHeigh){
		var temp=[];
		carNorthOneList[0]==null
		for(i=1;i<carNorthTwoList.length;i++){
			temp[i-1]=carNorthTwoList[i]
		}
		carNorthTwoList=temp
		temp=null
	}
	
	if(carEastOneList.length>0&&carEastOneList[0].carPointx<-60){
		var temp=[];
		carNorthOneList[0]==null
		for(i=1;i<carEastOneList.length;i++){
			temp[i-1]=carEastOneList[i]
		}
		carEastOneList=temp
		temp=null
	}
	if(carEastTwoList.length>0&&carEastTwoList[0].carPointx<-60){
		var temp=[];
		carNorthOneList[0]==null
		for(i=1;i<carEastTwoList.length;i++){
			temp[i-1]=carEastTwoList[i]
		}
		carEastTwoList=temp
		temp=null
	}
	
	if(carSouthOneList.length>0&&carSouthOneList[0].carPointy<-60){
		var temp=[];
		carNorthOneList[0]==null
		for(i=1;i<carSouthOneList.length;i++){
			temp[i-1]=carSouthOneList[i]
		}
		carSouthOneList=temp
		temp=null
	}
	
	if(carSouthTwoList.length>0&&carSouthTwoList[0].carPointy<-60){
		var temp=[];
		carNorthOneList[0]==null
		for(i=1;i<carSouthTwoList.length;i++){
			temp[i-1]=carSouthTwoList[i]
		}
		carSouthTwoList=temp
		temp=null
	}
	
	if(carWestOneList.length>0&&carWestOneList[0].carPointx>_this.canvasWidth){
		var temp=[];
		carNorthOneList[0]==null
		for(i=1;i<carWestOneList.length;i++){
			temp[i-1]=carWestOneList[i]
		}
		carWestOneList=temp
		temp=null
	}
	if(carWestTwoList.length>0&&carWestTwoList[0].carPointx>_this.canvasWidth){
		var temp=[];
		carNorthOneList[0]==null
		for(i=1;i<carWestTwoList.length;i++){
			temp[i-1]=carWestTwoList[i]
		}
		carWestTwoList=temp
		temp=null
	}
	
	_this=null
	}
	switch(carDire){
		case carDirection[0]:
		case carDirection[2]:
		      obj.imgWidth=_this.carHeigh
		      obj.imgHeigh=_this.carWidth
			  break;
	    case  carDirection[1]:
	    case  carDirection[3]:
	          obj.imgWidth=_this.carWidth
		      obj.imgHeigh=_this.carHeigh
		      break;
	}
	return obj
}

//全局变量，游戏控制需要参数，方法
var carNumber=0
var carNorthOneList=[]
var	carNorthTwoList=[]
var	carEastOneList=[]
var	carEastTwoList=[]
var	carSouthOneList=[]
var	carSouthTwoList=[]
var	carWestOneList=[]
var	carWestTwoList=[]
var carType=["red","gray","yellow"]  //车的类型
var carDirection=["north","east","south","west"]
var i
var j
var SpeedType=2
var AddSpeed=7
var deviation=3
var flashTime=60

var gameMonitor={
	canvasWidth:320,   //canvas的宽度
	canvasHeigh:568,   //canvas的高度
	carWidth:60,        //车的宽度
	carHeigh:30,        //车的高度
	roadWidth:40,        //路的宽度
	pointCar:null,    //鼠标指向的车的信息
	timmer:null,       //自动循环动画帧
	startPoint:{
		x:0,
		y:0
	},
	endPoint:{
		x:0,
		y:0
	},
	
	chink:5,   //车与路之间的空隙
	
	
	eventType:{
		start:'touchstart',
		move:'touchmove',
		end:'touchend'
	},
	init:function(){
		var _this=this
		var canvas=document.getElementById('game')
		var ctx=canvas.getContext('2d')
		_this.initListener(canvas,ctx);
		_this=null
		canvas=null
		ctx=null
		
	},
	initListener:function(canvas,ctx){
		  var _this=gameMonitor
		canvas.addEventListener(gameMonitor.eventType.start,function(evt){
			evt.preventDefault();
			
			gameMonitor.startPoint=getMousePos(canvas,evt)
			gameMonitor.pointCar=getThisCar(gameMonitor.startPoint.x,gameMonitor.startPoint.y).id//获得该点的车辆
		})
		
		canvas.addEventListener(gameMonitor.eventType.end,function(evt){
			evt.preventDefault();
			
			gameMonitor.endPoint=getMousePos(canvas,evt)
			if(gameMonitor.pointCar!=null){
				changCarSpeed()  //改变该车的速度
			}
			gameMonitor.pointCar==null
		})
		
		canvas.addEventListener(gameMonitor.eventType.move,function(evt){
			evt.preventDefault();
			gameMonitor.endPoint=getMousePos(canvas,evt)
			if(gameMonitor.pointCar!=null){
				changCarSpeed()  //改变该车的速度
			}
			gameMonitor.pointCar==null
		})
		
		
		_this.run(ctx)
		_this=null
	},
	reset : function(ctx){
		carNorthOneList=[]
		carNorthTwoList=[]
		carEastOneList=[]
		carEastTwoList=[]
		carSouthOneList=[]
		carSouthTwoList=[]
		carWestOneList=[]
		carWestTwoList=[]
		carNumber=0
		this.pointCar=null
		this.timmer = setTimeout(function(){
			gameMonitor.run(ctx)
			
		},Math.round(1000/flashTime));
		
		
	},
	run:function(ctx){
		var _this=gameMonitor;
		ctx.clearRect(0,0,_this.canvasWidth,_this.canvasHeigh);
		
		genorateCar() //随机产生车辆
		
		paintCarList(ctx)//绘制车辆
		
		crashCars()
		_this.timmer=setTimeout(function(){
			gameMonitor.run(ctx)
			
		},Math.round(1000/60))

		_this=null
	},
	isMobile : function(){
		var sUserAgent= navigator.userAgent.toLowerCase(),
		bIsIpad= sUserAgent.match(/ipad/i) == "ipad",
		bIsIphoneOs= sUserAgent.match(/iphone os/i) == "iphone os",
		bIsMidp= sUserAgent.match(/midp/i) == "midp",
		bIsUc7= sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4",
		bIsUc= sUserAgent.match(/ucweb/i) == "ucweb",
		bIsAndroid= sUserAgent.match(/android/i) == "android",
		bIsCE= sUserAgent.match(/windows ce/i) == "windows ce",
		bIsWM= sUserAgent.match(/windows mobile/i) == "windows mobile",
		bIsWebview = sUserAgent.match(/webview/i) == "webview";
		return (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM);
     }
} 

//---------全局变量游戏的控制
if(!gameMonitor.isMobile()){
	gameMonitor.eventType.start = 'mousedown';
	gameMonitor.eventType.move = 'onmousedown';
	gameMonitor.eventType.end = 'mouseup';
}
var sUserAgent= navigator.userAgent.toLowerCase()
if(sUserAgent.match(/android/i) == "android"){
	alert("android")
	SpeedType=3
	AddSpeed=14
	flashTime=60
}



//-----------一些全局函数的定义
//--------获得canvas中鼠标所在的位置
 function getMousePos(canvas, evt) { 
   var rect = canvas.getBoundingClientRect(); 
   
   if(gameMonitor.isMobile()){
   	return { 
     x: evt.changedTouches[0].clientX - rect.left * (canvas.width / rect.width),
     y: evt.changedTouches[0].clientY - rect.top * (canvas.height / rect.height)
   }
   }
   else{
   	return { 
     x: evt.clientX - rect.left * (canvas.width / rect.width),
     y: evt.clientY - rect.top * (canvas.height / rect.height)
   }
   }
   
   rect=null
 }
 
 //-----------返回鼠标点击的车辆
 function getThisCar(x,y){
 	var _this=gameMonitor
 	var clickCar=null
 	if(x>_this.canvasWidth/2-_this.roadWidth*2-10*2&&x<_this.canvasWidth/2-_this.roadWidth+10){
 		//0
 		for(i=0;i<carNorthOneList.length;i++){
 			if(inCarNS(carNorthOneList[i],y)){
 				clickCar=carNorthOneList[i]
 				
 				
 			}
 		}
 	}
 	if(x>_this.canvasWidth/2-_this.roadWidth-10&&x<_this.canvasWidth/2+10){
 		//1
 		for(i=0;i<carNorthTwoList.length;i++){
 			if(inCarNS(carNorthTwoList[i],y)){
 				clickCar=carNorthTwoList[i]
 				
 			}
 		}
 	}
 	if(y>_this.canvasHeigh/2-_this.roadWidth*2-10&&y<_this.canvasHeigh/2-_this.roadWidth+10){
 		//2
 		for(i=0;i<carEastOneList.length;i++){
 			if(inCarEW(carEastOneList[i],x)){
 				clickCar=carEastOneList[i]
 				
 			}
 		}
 	}
 	if(y>_this.canvasHeigh/2-_this.roadWidth-10&&y<_this.canvasHeigh/2+10){
 		//3
 		for(i=0;i<carEastTwoList.length;i++){
 			if(inCarEW(carEastTwoList[i],x)){
 				clickCar=carEastTwoList[i]
 				
 			}
 		}
 	}
 	if(x>_this.canvasWidth/2+_this.roadWidth-10&&x<_this.canvasWidth/2+_this.roadWidth*2+10){
 		//4
 		for(i=0;i<carSouthOneList.length;i++){
 			if(inCarNS(carSouthOneList[i],y)){
 				clickCar=carSouthOneList[i]
 				
 			}
 		}
 	}
 	if(x>_this.canvasWidth/2-10&&x<_this.canvasWidth/2+_this.roadWidth+10){
 		//5
 		for(i=0;i<carSouthTwoList.length;i++){
 			if(inCarNS(carSouthTwoList[i],y)){
 				clickCar=carSouthTwoList[i]
 				
 			}
 		}
 	}
 	if(y>_this.canvasHeigh/2+_this.roadWidth-10&&y<_this.canvasHeigh/2+_this.roadWidth*2+10){
 		//6
 		
 		for(i=0;i<carWestOneList.length;i++){
 			if(inCarEW(carWestOneList[i],x)){
 				clickCar=carWestOneList[i]
 				
 			}
 		}
 	}
 	if(y>_this.canvasHeigh/2-10&&y<_this.canvasHeigh/2+_this.roadWidth+10){
 		//7
 		for(i=0;i<carWestTwoList.length;i++){
 			if(inCarEW(carWestTwoList[i],x)){
 				clickCar=carWestTwoList[i]
 				
 			}
 		}
 	}
 	
 	_this=null
 	return clickCar
 }
 //------------改变鼠标点击的车辆的速度
 function changCarSpeed(){
 	//if 小于一定值则speed=0或者等于原来的speed
 	//if 不在一个方向 不操作
 	//if 在一个方向  则加速
   	if((gameMonitor.startPoint.y-gameMonitor.endPoint.y)>10){
					//south
		for(i=0;i<carSouthOneList.length;i++){
			if(gameMonitor.pointCar==carSouthOneList[i].id){
				carSouthOneList[i].speed=AddSpeed
				gameMonitor.carPointx=0
				gameMonitor.carPointy=0
				return;
				}
		}
		for(i=0;i<carSouthTwoList.length;i++){
			if(gameMonitor.pointCar==carSouthTwoList[i].id){
				carSouthTwoList[i].speed=AddSpeed
				gameMonitor.carPointx=0
				gameMonitor.carPointy=0
				return;
			}
		}
	}
	if((gameMonitor.endPoint.y-gameMonitor.startPoint.y)>10){
		for(i=0;i<carNorthOneList.length;i++){
			if(gameMonitor.pointCar==carNorthOneList[i].id){
				carNorthOneList[i].speed=AddSpeed
				gameMonitor.carPointx=0
				gameMonitor.carPointy=0
				return;
				}
		}
		for(i=0;i<carNorthTwoList.length;i++){
			if(gameMonitor.pointCar==carNorthTwoList[i].id){
				carNorthTwoList[i].speed=AddSpeed
				gameMonitor.carPointx=0
				gameMonitor.carPointy=0
				return;
			}
		}			
	}
	if((gameMonitor.endPoint.x-gameMonitor.startPoint.x)>10){
		for(i=0;i<carWestOneList.length;i++){
			if(gameMonitor.pointCar==carWestOneList[i].id){
				carWestOneList[i].speed=AddSpeed
				gameMonitor.carPointx=0
				gameMonitor.carPointy=0
				return;
				}
		}
		for(i=0;i<carWestTwoList.length;i++){
			if(gameMonitor.pointCar==carWestTwoList[i].id){
				carWestTwoList[i].speed=AddSpeed
				gameMonitor.carPointx=0
				gameMonitor.carPointy=0
				return;
			}
		}			
	}
	if((gameMonitor.startPoint.x-gameMonitor.endPoint.x)>10){
		for(i=0;i<carEastOneList.length;i++){
			if(gameMonitor.pointCar==carEastOneList[i].id){
				carEastOneList[i].speed=AddSpeed
				gameMonitor.carPointx=0
				gameMonitor.carPointy=0
				return;
				}
		}
		for(i=0;i<carEastTwoList.length;i++){
			if(gameMonitor.pointCar==carEastTwoList[i].id){
				carEastTwoList[i].speed=AddSpeed
				gameMonitor.carPointx=0
				gameMonitor.carPointy=0
				return;
			}
		}			
	}
 	
 	//----------停车
 	if(Math.abs((gameMonitor.startPoint.y-gameMonitor.endPoint.y))<10){
					//south
		for(i=0;i<carSouthOneList.length;i++){
			if(gameMonitor.pointCar==carSouthOneList[i].id){
				carSouthOneList[i].speed=0
				//carSouthOneList[i].isstop+=1
				
				return;
				}
		}
		for(i=0;i<carSouthTwoList.length;i++){
			if(gameMonitor.pointCar==carSouthTwoList[i].id){
				carSouthTwoList[i].speed=0
				//carSouthTwoList[i].isstop+=1
				return;
			}
		}
	}
	if(Math.abs((gameMonitor.endPoint.y-gameMonitor.startPoint.y))<10){
		for(i=0;i<carNorthOneList.length;i++){
			if(gameMonitor.pointCar==carNorthOneList[i].id){
				carNorthOneList[i].speed=0
				//carNorthOneList[i].isstop+=1
				return;
				}
		}
		for(i=0;i<carNorthTwoList.length;i++){
			if(gameMonitor.pointCar==carNorthTwoList[i].id){
				carNorthTwoList[i].speed=0
				//carNorthTwoList[i].isstop+=1
				return;
			}
		}			
	}
	if(Math.abs(gameMonitor.endPoint.x-gameMonitor.startPoint.x)<10){
		for(i=0;i<carWestOneList.length;i++){
			if(gameMonitor.pointCar==carWestOneList[i].id){
				carWestOneList[i].speed=0
				//carWestOneList[i].isstop+=1
				return;
				}
		}
		for(i=0;i<carWestTwoList.length;i++){
			if(gameMonitor.pointCar==carWestTwoList[i].id){
				carWestTwoList[i].speed=0
				//carWestTwoList[i].isstop+=1
				return;
			}
		}			
	}
	if(Math.abs(gameMonitor.startPoint.x-gameMonitor.endPoint.x)<10){
		for(i=0;i<carEastOneList.length;i++){
			if(gameMonitor.pointCar==carEastOneList[i].id){
				carEastOneList[i].speed=0
				//carEastOneList[i].isstop+=1
				return;
				}
		}
		for(i=0;i<carEastTwoList.length;i++){
			if(gameMonitor.pointCar==carEastTwoList[i].id){
				carEastTwoList[i].speed=0
				//carEastTwoList[i].isstop+=1
				return;
			}
		}			
	}
 	
 }
//---------绘制车辆
function paintCarList(ctx){
     var _this=gameMonitor
     var car
     for(i=0;i<carNorthOneList.length;i++){
     	car=carNorthOneList[i];
     	if(car){
     		car.paint(ctx)
     	    car.move(ctx)
     	}
     }
     
     for(i=0;i<carNorthTwoList.length;i++){
     	car=carNorthTwoList[i];
     	if(car){
     		car.paint(ctx)
     	    car.move(ctx)
     	}
     }
     
     for(i=0;i<carEastOneList.length;i++){
     	car=carEastOneList[i];
     	if(car){
     		car.paint(ctx)
     	    car.move(ctx)
     	}
     }
     
     for(i=0;i<carEastTwoList.length;i++){
     	car=carEastTwoList[i];
     	if(car){
     		car.paint(ctx)
     	    car.move(ctx)
     	}
     }
     
     for(i=0;i<carSouthOneList.length;i++){
     	car=carSouthOneList[i];
     	if(car){
     		car.paint(ctx)
     	    car.move(ctx)
     	}
     }
     
     for(i=0;i<carSouthTwoList.length;i++){
     	car=carSouthTwoList[i];
     	if(car){
     		car.paint(ctx)
     	    car.move(ctx)
     	}
     }
     
     for(i=0;i<carWestOneList.length;i++){
     	car=carWestOneList[i];
     	if(car){
     		car.paint(ctx)
     	    car.move(ctx)
     	}
     }
     
     for(i=0;i<carWestTwoList.length;i++){
     	car=carWestTwoList[i];
     	if(car){
     		car.paint(ctx)
     	    car.move(ctx)
     	}
     }
     car=null
     _this=null
}
//---------随机产生车辆
function genorateCar() {
	    var _this=gameMonitor
		var carColor
		var carDire
		var carNum
	    var genRate=10;
	
	var radom=parseInt(Math.random()*40);
	if(radom==genRate){//产生车辆
		carColor=carType[parseInt(Math.random()*3)]  //0-2
		carDire=carDirection[parseInt(Math.random()*4)]  //0-3
		carNum=parseInt(Math.random()*2)  //0-1
		var car=new Object()
		car=Car(carColor,carDire,carNum)
		switch(carDire){
			case carDirection[0]:
			if(carNum==0){
				if(carNorthOneList.length==0||carNorthOneList[carNorthOneList.length-1].carPointy>0){
					carNorthOneList.push(car)
				}
			}else{
				if(carNorthTwoList.length==0||carNorthTwoList[carNorthTwoList.length-1].carPointy>0){
					carNorthTwoList.push(car)
				}
				
			}
			break;
			
			case carDirection[1]:
			if(carNum==0){
				if(carEastOneList.length==0||carEastOneList[carEastOneList.length-1].carPointx<_this.canvasWidth){
					carEastOneList.push(car)
				}
			}else{
				if(carEastTwoList.length==0||carEastTwoList[carEastTwoList.length-1].carPointx<_this.canvasWidth){
					carEastTwoList.push(car)
				}
			}
			break;
			
			case carDirection[2]:
			if(carNum==0){
				if(carSouthOneList.length==0||carSouthOneList[carSouthOneList.length-1].carPointy<_this.canvasHeigh){
					carSouthOneList.push(car)
				}
			}else{
				if(carSouthTwoList.length==0||carSouthTwoList[carSouthTwoList.length-1].carPointy<_this.canvasHeigh){
					carSouthTwoList.push(car)
				}
			}
			break;
			
			case carDirection[3]:
			if(carNum==0){
				if(carWestOneList.length==0||carWestOneList[carWestOneList.length-1].carPointx>0){
					carWestOneList.push(car)
				}
			}else{
				if(carWestTwoList.length==0||carWestTwoList[carWestTwoList.length-1].carPointx>0){
					carWestTwoList.push(car)
				}
			}
			break;
		}
		car=null
		
	}
	  _this=null
		 carColor=null
		 carDire=null
		 carNum=null
	     genRate=null
	     radom=null
}
//-------返回车辆初始化坐标
function returnPoint(direction,num){
	var _this=gameMonitor
	var point={
		x:0,
		y:0
	}
	
	switch(direction){
		case carDirection[0]:
		   if(num==0){
		       point.x=_this.canvasWidth/2-_this.roadWidth*2+_this.chink
		       point.y=0-_this.carWidth*2
		   }else{
		   	   point.x=_this.canvasWidth/2-_this.roadWidth+_this.chink
		       point.y=0-_this.carWidth*2
		   }
		   break;
		
		case carDirection[1]:
		    if(num==0){
		    	point.x=_this.canvasWidth+_this.carWidth
		    	point.y=_this.canvasHeigh/2-_this.roadWidth*2+_this.chink
		    }else{
		    	point.x=_this.canvasWidth+_this.carWidth
		    	point.y=_this.canvasHeigh/2-_this.roadWidth+_this.chink
		    }
		    break;
		    
		case carDirection[2]:
		    if(num==0){
		    	point.x=_this.canvasWidth/2+_this.roadWidth+_this.chink
		    	point.y=_this.canvasHeigh+_this.carWidth
		    }else{
		    	point.x=_this.canvasWidth/2+_this.chink
		    	point.y=_this.canvasHeigh+_this.carWidth
		    }
		    break;
		    
		case carDirection[3]:
		    if(num==0){
		    	point.x=0-_this.carWidth*2
		    	point.y=_this.canvasHeigh/2+_this.roadWidth+_this.chink
		    }else{
		    	point.x=0-_this.carWidth*2
		    	point.y=_this.canvasHeigh/2+_this.chink
		    }
		    break;
	}
    _this=null
	return point
}
//-----判断该点是否在车上
function inCarNS(car,y){
	if((car.carPointy-5)<y&&(car.carPointy+65)>y){
		return true
	}
	return false
}
function inCarEW(car,x){
	if(x>(car.carPointx-5)&&x<car.carPointx+65){
		return true
	}
	return false
}


//-------------撞车检测
function crashCars(){
	_this=gameMonitor
	//north1
	//与自己相比
	for(i=0;i<carNorthOneList.length;i++){
		for(j=i+1;j<carNorthOneList.length;j++){
			if(carNorthOneList[i].carPointy<(carNorthOneList[j].carPointy+_this.carWidth+5))
			stop()
			
		}
	}
	//与横向两端做比较
	//
	 compareCrashS(carNorthOneList,carEastOneList)
	 compareCrashS(carNorthOneList,carEastTwoList)
	 compareCrashS(carNorthOneList,carWestOneList)
	 compareCrashS(carNorthOneList,carWestTwoList)
	
	//north2
	//与自己相比
	for(i=0;i<carNorthTwoList.length;i++){
		for(j=i+1;j<carNorthTwoList.length;j++){
			if(carNorthTwoList[i].carPointy<(carNorthTwoList[j].carPointy+_this.carWidth+5))
			stop()
			
		}
	}
     compareCrashS(carNorthTwoList,carEastOneList)
	 compareCrashS(carNorthTwoList,carEastTwoList)
	 compareCrashS(carNorthTwoList,carWestOneList)
	 compareCrashS(carNorthTwoList,carWestTwoList)
	
	//east1
	//与自己相比
	for(i=0;i<carEastOneList.length;i++){
		for(j=i+1;j<carEastOneList.length;j++){
			if(carEastOneList[i].carPointx>(carEastOneList[j].carPointx-_this.carWidth-5))
			stop()
			
		}
	}
	
     compareCrashE(carEastOneList,carNorthOneList)	
     compareCrashE(carEastOneList,carNorthTwoList)
     compareCrashE(carEastOneList,carSouthOneList)
     compareCrashE(carEastOneList,carSouthOneList)
	//east2
	//与自己相比
	for(i=0;i<carEastTwoList.length;i++){
		for(j=i+1;j<carEastTwoList.length;j++){
			if(carEastTwoList[i].carPointx>(carEastTwoList[j].carPointx-_this.carWidth-5))
			stop()
			
		}
	}
	

     compareCrashE(carEastTwoList,carNorthOneList)	
     compareCrashE(carEastTwoList,carNorthTwoList)
     compareCrashE(carEastTwoList,carSouthOneList)
     compareCrashE(carEastTwoList,carSouthOneList)
	//south1
	//与自己相比
	for(i=0;i<carSouthOneList.length;i++){
		for(j=i+1;j<carSouthOneList.length;j++){
			if(carSouthOneList[i].carPointy>(carSouthOneList[j].carPointy-_this.carWidth-5))
			stop()
			
		}
	}
	
	compareCrashS(carSouthOneList,carEastOneList)
	compareCrashS(carSouthOneList,carEastTwoList)
	compareCrashS(carSouthOneList,carWestOneList)
	compareCrashS(carSouthOneList,carWestTwoList)
	//south2
	//与自己相比
	for(i=0;i<carSouthTwoList.length;i++){
		for(j=i+1;j<carSouthTwoList.length;j++){
			if(carSouthTwoList[i].carPointy>(carSouthTwoList[j].carPointy-_this.carWidth-5))
			stop()
			
		}
	}
	
	compareCrashS(carSouthTwoList,carEastOneList)
	compareCrashS(carSouthTwoList,carEastTwoList)
	compareCrashS(carSouthTwoList,carWestOneList)
	compareCrashS(carSouthTwoList,carWestTwoList)
	//west1
	//与自己相比
	for(i=0;i<carWestOneList.length;i++){
		for(j=i+1;j<carWestOneList.length;j++){
			if(carWestOneList[i].carPointx<(carWestOneList[j].carPointx+_this.carWidth-5))
			stop()
			
		}
	}
	
	compareCrashE(carWestOneList,carNorthOneList)
	compareCrashE(carWestOneList,carNorthTwoList)
	compareCrashE(carWestOneList,carSouthOneList)
	compareCrashE(carWestOneList,carSouthTwoList)
	
	//west2
	//与自己相比
	for(i=0;i<carWestTwoList.length;i++){
		for(j=i+1;j<carWestTwoList.length;j++){
			if(carWestTwoList[i].carPointx<(carWestTwoList[j].carPointx+_this.carWidth-5))
			stop()
			
		}
	}
	
	compareCrashE(carWestTwoList,carNorthOneList)
	compareCrashE(carWestTwoList,carNorthTwoList)
	compareCrashE(carWestTwoList,carSouthOneList)
	compareCrashE(carWestTwoList,carSouthTwoList)
	
	
}

//---------------对于不在统一水平线的车两两比较

//-------------
function compareCrashS(listS,listW){
	
	var top
	var bot
	var left
	var right
	
	if(listS.length>0&&listW.length>0){
		for(i=0;i<listS.length;i++){
		for(j=0;j<listW.length;j++){
			top=listW[j].carPointy-_this.carWidth+deviation+5
			bot=listW[j].carPointy+_this.carHeigh-deviation-5
			left=listW[j].carPointx-_this.carHeigh+deviation+5
			right=listW[j].carPointx+_this.carWidth-deviation-5
			if(listS[i].carPointy>top&&listS[i].carPointy<bot&&listS[i].carPointx>left&&listS[i].carPointx<right){
			
				stop()
			}
		}
	}
	}
	
	
	top=null
	bot=null
	left=null
	right=null
}
//-----------------
function compareCrashE(listE,listN){
	var top
	var bot
	var left
	var right
	
	if(listE.length>0&&listN.length>0){
		for(i=0;i<listE.length;i++){
			for(j=0;j<listN.length;j++){
				top=listN[j].carPointy-gameMonitor.carHeigh+deviation+5
				bot=listN[j].carPointy+gameMonitor.carWidth-deviation-5
				left=listN[j].carPointx-gameMonitor.carWidth+deviation+5
				right=listN[j].carPointx+gameMonitor.carHeigh-deviation-5
				if(listE[i].carPointy>top&&listE[i].carPointy<bot&&listE[i].carPointx>left&&listE[i].carPointx<right){
					stop()
					
					
				}
			}
		}
	}
	
	top=null
	bot=null
	left=null
	right=null
}
//-------------------

//------------------------
//--------停止游戏
function stop(){
	setTimeout(function(){
		$("#gameOverPage").css("display","block")
			clearTimeout(gameMonitor.timmer);
		}, 0);
		
		setTimeout(function(){
							$('#gameOverPage').hide();
							$("#share").show();
							$("#score").html(carNumber-carSouthOneList.length-carSouthTwoList.length-
							carEastOneList.length-carEastTwoList.length-
							carNorthOneList.length-carNorthTwoList.length-
							carWestOneList.length-carWestTwoList.length
							)
						}, 2000);
}

//------------基于不同浏览器对drawImage的兼容
function preImage(url,callback){
	 var img = new Image(); //创建一个Image对象，实现图片的预下载
     img.src = url;
   
    if (img.complete) { // 如果图片已经存在于浏览器缓存，直接调用回调函数
         callback.call(img);
        return; // 直接返回，不用再处理onload事件
     }

     img.onload = function () { //图片下载完毕时异步调用callback函数。
         callback.call(img);//将回调函数的this替换为Image对象
     };
}

$(document).ready(function(){
	$("#startPage").click(function(){
		$(this).hide()
        
		gameMonitor.init()
	})
	
	$("#resect").click(function(){
		$("#gameOverPage").css("display","none")
		$("#share").hide();
		var canvas = document.getElementById('game');
			var ctx = canvas.getContext('2d');
			gameMonitor.reset(ctx )
			gameMonitor.run()
	})
	
	$("#tofriend").click(function(){
		$("#weixin").show()
		$("#share").hide()
	})
	
	$("#weixin").click(function(){
		$("#weixin").hide()
		$("#share").show()
	})
})

